package signal;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.webrtc.DataChannel;
import org.webrtc.PeerConnection;
import org.webrtc.PeerConnectionFactory;

import java.util.ArrayList;
import java.util.List;

import webrtcwrappers.IceCandidateInit;
import webrtcwrappers.PeerConnectionCreator;

/**
 * @author namvdo
 */
public class Transport {
    private static final Logger logger = LoggerFactory.getLogger(Transport.class);
    public static final String API_CHANNEL = "ion-sfu";
    private final Signal signal;
    private final PeerConnectionCreator peerCreator;
    private List<IceCandidateInit> candidates;

    public Transport(Role role, Signal signal, PeerConnection.RTCConfiguration config, PeerConnectionFactory peerFactory) {
        this.signal = signal;
        this.peerCreator = new PeerConnectionCreator(role, signal, config, peerFactory, candidates);
        this.candidates = new ArrayList<>();
        DataChannel.Init dataChannelInit = new DataChannel.Init();
        dataChannelInit.maxRetransmits = 30;
        if (Role.PUBLISHER == role) {
            logger.info("Create a data channel");
            this.peerCreator.getPc().createDataChannel(API_CHANNEL, dataChannelInit);
        }
    }


    public Signal getSignal() {
        return signal;
    }

    public PeerConnectionCreator getPeerConnectionCreator() {
        return peerCreator;
    }

    public List<IceCandidateInit> getCandidates() {
        return candidates;
    }


    public void setCandidates(List<IceCandidateInit> candidates) {
        this.candidates = candidates;
    }
}
